import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ModalController, NavController } from '@ionic/angular';
import { PrompBoxtModal } from '@modals/prompt-box/prompt-box';
import { InstallService } from '@services/data.service/index';
import { MyNoteService } from '@services/myNote.service';
import { CacheService } from '@services/cache.service';
import { EFOSHttpInterceptor } from '@services/http.interceptor.service';


@Component({
  selector: 'active-step',
  templateUrl: './active-step.html',
  styleUrls: ['active-step.scss'],
  providers: [EFOSHttpInterceptor]
})
export class ActiveComponent implements OnInit {
  @Input() roomInfo;
  @Input() installData;
  @Input() Step;
  @Output() NextNum: EventEmitter<any> = new EventEmitter();
  begin;
  finish;
  failed;
  CodeInfo;
  timeInterval; // 判断是否超时
  click = false; // 防止重复点击
  constructor(public modalCtrl: ModalController, public installService: InstallService,
    private myNote: MyNoteService, private cache: CacheService, private navCtrl: NavController,
    private http: EFOSHttpInterceptor) {
  }

  // 初始页面显示
  ngOnInit() {
    console.log('激活页面房间信息', this.roomInfo);
    this.Step.state = 1;
    this.begin = this.finish = this.failed = false;
    setTimeout(() => {
      this.click = true;
    }, 1500);
  }

  // 激活成功与失败
  active() {
    this.begin = true;
    this.finish = this.failed = false;
    if (this.roomInfo.Type === 0) {
      console.log('激活数据栈');
      this.installService.getActivate(this.roomInfo.device.areaId, this.roomInfo.device.stackId, this.installData.Code).subscribe(res => {
        this.isActive(res);
      });
    } else {
      console.log('激活仪表');
      this.installService.getActivateMeter(this.roomInfo.parameter.UID,
        this.roomInfo.device.meterId, this.roomInfo.device.pid, this.installData.Address, this.roomInfo.Relation ? this.roomInfo.Relation.deviceParams : null, this.roomInfo.Relation ? this.roomInfo.Relation.addtions : null).subscribe(res => {
          this.isActive(res);
        });
    }
  }

  // 切换步骤
  toNext(i) {
    this.click = false;
    this.Step.now = i;
    this.installData.step = 4;
    this.installData.data4 = this.CodeInfo;
    this.installService.saveInstallData(this.installData.areaId, this.installData.meterId, this.installData);
    this.NextNum.emit(this.Step.now);
    setTimeout(() => {
      this.click = true;
    }, 1000)
  }

  // 重新安装
  async change(i) {
    const modal = await this.modalCtrl.create({
      component: PrompBoxtModal
    });
    modal.present();
    const res = await modal.onDidDismiss();
    if (res.data) {
      this.installService.saveInstallData(this.installData.areaId, this.installData.meterId, null);
      this.navCtrl.pop();
    }
  }

  isActive(res) {
    if (res.success) {
      this.installData.isActive = res.code;
      if (this.installData.isActive === 200) {
        setTimeout(() => {
          this.finish = true;
        }, 2000);
      } else {
        setTimeout(() => {
          this.finish = false;
          this.failed = true;
        }, 2000);
      }
    } else {
      this.myNote.MyNotice(res.message);
      setTimeout(() => {
        this.finish = false;
        this.failed = true;
      }, 2000);
    }
  }

}
